<?php

namespace Admin\Controller;

/**
 * 合作伙伴、银行 控制器
 * 
 * @author BoBo
 *
 */
class LinkController extends CommController {
	
	/**
	 * 构造函数
	 */
	public function _initialize() {
		parent::_initialize ();
		// 模型
		$this->model = D ( 'Link' );
		// 参数
		$this->paramter ['pid'] = I ( 'request.pid', 0 );
	}

	/**
	 * 前置添加方法
	 */
	public function _before_add() {
		$this->thisBefore();
	}

	/**
	 * 前置编辑方法
	 */
	public function _before_edit() {
		$this->thisBefore();
	}
	
	/**
	 * 前置方法公用
	 */
	private function thisBefore() {
		$this->paramter ['model'] = I ( 'request.model', '' );
		$this->assign( 'paramter', $this->paramter );
	}

	/**
	 * 插件一条数据
	 */
	public function create() {
		$modelName = I('post.model');
		$type = (int)$_POST['type'];
		$refresh = $_POST ['refresh'] ? $_POST ['refresh'] : 'refreshClose';
		$return = $this->model->commInsert ( $_POST );
		$method = $type == 1 ? 'bankList' : 'courierList';
		$this->dwzSuccess ( $return ['info'], $return ['status'], $method, $refresh );
	}

	/**
	 * 修改一条数据
	 */
	public function update() {
		$modelName = I('post.model');
		$refresh = $_POST ['refresh'] ? $_POST ['refresh'] : 'refreshClose';
		$return = $this->model->commUpdate ( $_POST );
		$this->dwzSuccess ( $return ['info'], $return ['status'], $modelName, $refresh );
	}

	/**
	 * 删除一条或一组数据(伪删除)
	 */
	public function remove() {
		$idList = I ( 'post.id' );
		$idArr = explode(',', $idList);
		$linkData = D('Link')->field('type')->where(array('id'=>$idArr[0]))->find();
		$modelName = $linkData['type'] == 1 ? 'bankList' : 'courierList';
		$return = $this->model->commRemove ( $idList );
		$this->dwzSuccess ( $return ['info'], $return ['status'], $modelName, "refreshThis" );
	}

	/**
	 * 设置一条或一组数据的状态
	 */
	public function commSetStatus() {
		$idList = $_REQUEST ['id'];
		$param = I ( 'get.param' );
		$idArr = explode(',', $idList);
		$linkData = D('Link')->field('type')->where(array('id'=>$idArr[0]))->find();
		$modelName = $linkData['type'] == 1 ? 'bankList' : 'courierList';
		$return = $this->model->commSetStatus ( $idList, explode ( "-", $param ) );
		$this->dwzSuccess ( $return ['info'], $return ['status'], $modelName, "refreshThis" );
	}
	
	/**
	 * 合作银行列表
	 */
	public function bankList() {
		// 设置查询条件
		$sql = "select a.* from tm_link a where a.type = 1";

		// 读取数据
		$this->dataList = $this->getPageSQLList( $this->model, $sql );
		$this->assign ( 'dataList', $this->dataList );
		$this->assign ( 'type', 1 );
		// 渲染模板
		$this->display ( 'Link/index' );
	}
	
	/**
	 * 合作伙伴列表
	 */
	public function courierList() {
		// 设置查询条件
		$sql = "select a.* from tm_link a where a.type = 2";

		// 读取数据
		$this->dataList = $this->getPageSQLList( $this->model, $sql );
		$this->assign ( 'dataList', $this->dataList );
		$this->assign ( 'type', 2 );
		
		// 渲染模板
		$this->display ( 'Link/index' );
	}
}